package org.android.ca;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ListActivity extends Activity implements OnItemClickListener{
	List<Model> list;
	TextView text;


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		list = new ArrayList<Model>();
		Intent intent = getIntent();
		 Uri data = intent.getData();
	     Bundle extras=getIntent().getExtras();
		//String[] values=extras.getStringArray("listdata");
		StringBuilder b = new StringBuilder();
		try {
			HttpGet httpget = new HttpGet(new URI(data.toString()));
			HttpResponse response = new DefaultHttpClient().execute(httpget);
			
			InputStream inp = response.getEntity().getContent();
			BufferedReader rd = new BufferedReader(new InputStreamReader(inp));
			String line;
			while ((line = rd.readLine()) != null) {
				String[] st=line.split(",");
				list.add(new Model(st[0],st[1],st[2],st[3],st[4],st[5],st[6]));
					
			}
			text.setText(data.toString());
		} catch (Exception e) {
			text.setText(e.toString());
		}
		 ListView t = (ListView) findViewById(R.id.listView1);
	        t.setAdapter(new SimpleAdapter(this, list, R.layout.row,
	          new String[]{"LeaveType","From","To"},
	          new int[]{ R.id.textView1}));
	     }
	


	@Override
	public void onItemClick(AdapterView<?> av, View v, int position,
			long id) {
		String item=(String)av.getAdapter().getItem(position);
		Toast.makeText(getApplicationContext(),item+"selected",Toast.LENGTH_LONG).show();
		Intent i=new Intent(getApplicationContext(), DetailsActivity.class);
		i.putExtra("name","st[0]");
		i.putExtra("LeaveId","st[1]");
		i.putExtra("LeaveType", "st[2]");
		i.putExtra("From", "st[3]");
		i.putExtra("To", "st[4]");
		i.putExtra("Status", "st[5]");
		i.putExtra("approvedBy", "st[6]");
		startActivity(i);

	}

}


